package com.example.game_community.controller;

import com.example.game_community.entity.User;
import com.example.game_community.service.UserService;
import com.example.game_community.utils.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/user")
public class UserController {

    @Autowired
    private UserService userService;

    @PostMapping("/login")
    public Result<User> login(@RequestBody User user) {
        User data = userService.login(user);
       if (data != null) {
           return Result.success(data);
       }else {
           return Result.error("用户密码错误或账号不存在！");
       }
    }

//    获取手机号验证码
    @GetMapping("/getPhoneCode")
    public Result<User> getPhoneCode(@RequestParam String phone) {
        return userService.sendVerCode(phone);
    }

//    用手机号登录，验证验证码
    @PostMapping("/sendCode")
    public Result<?> sendCode(@RequestParam String phone, @RequestParam String code) {
        return userService.verifyCode(phone,code);
    }

//    注册
    @PostMapping("/register")
    public Result<User> register(@RequestBody User user) {
        return userService.register(user);
    }

}
